<template>
  <div class="app-container">
    <!-- 搜索 -->
    <el-card class="box-card">
      <el-row type="flex" align="middle">
        <el-col :span="12">
          <div class="grid-content bg-purple search">
            <el-form ref="query" :model="query" label-width="100px">
              <el-form-item style="margin-bottom:0px" label="分类">
                <i slot="label" class="el-icon-search" style="font-size:20px"></i>
                <el-input style="width:360px" v-model="query.keywords" placeholder="请输入分类名称"></el-input>
              </el-form-item>
            </el-form>
          </div>
        </el-col>
        <el-col :span="12">
          <div class="grid-content bg-purple-light" style="text-align:right">
            <el-button type="primary" @click="queryAsk">查询</el-button>
            <el-button @click="queryReset">重置</el-button>
          </div>
        </el-col>
      </el-row>
    </el-card>
    <!-- 表格 -->
    <el-card class="box-card">
      <img
        class="watermark"
        v-if="baseApi== 'https://testouhun.wm2177.com'"
        style="right: 0;"
        src="@/assets/watermark.png"
      />

      <el-button type="primary" style="margin-bottom:20px" @click="addList">添加</el-button>
      <el-table
        :data="tableData"
        :header-cell-style="{background:'#eee'}"
        border
        style="width: 100%;margin-bottom: 20px;"
        v-loading="loading"
      >
        <el-table-column align="center" type="index" width="50" label="序号"></el-table-column>
        <el-table-column align="center" prop="class_name" label="名称"></el-table-column>
        <el-table-column align="center" prop="sort" label="排序"></el-table-column>
        <el-table-column align="center" prop="add_time" label="添加时间"></el-table-column>
        <el-table-column align="center" prop="update_time" label="更新时间"></el-table-column>
        <el-table-column align="center" fixed="right" label="操作" width="150">
          <template slot-scope="scope">
            <el-button @click="handleEdite(scope.row)" type="text" size="small">编辑</el-button>
            <el-popconfirm
              confirmButtonText="好的"
              cancelButtonText="不用了"
              icon="el-icon-info"
              iconColor="red"
              title="这是一段内容确定删除吗？"
              @onConfirm="handleDel(scope.row)"
            >
              <el-button
                style="margin-left:10px;color:red"
                slot="reference"
                type="text"
                size="small"
              >删除</el-button>
            </el-popconfirm>
          </template>
        </el-table-column>
      </el-table>
      <div style="text-align:right">
        <el-pagination
          background
          layout="prev, pager, next"
          :current-page="currentPage"
          :total="total"
          @current-change="currentChange"
        ></el-pagination>
      </div>
    </el-card>
    <!-- 抽屉 -->
    <el-drawer
      :title="title"
      :visible.sync="drawer"
      :direction="direction"
      :before-close="handleClose"
    >
      <el-form ref="form" :model="form" :rules="rules" label-width="100px" style="margin-left:50px">
        <el-form-item label="分类名称" prop="class_name">
          <el-input style="width:360px" v-model="form.class_name" placeholder="请输入分类名称"></el-input>
        </el-form-item>
        <el-form-item label="排序" prop="sort">
          <el-input style="width:360px" v-model="form.sort" placeholder="请输入排序"></el-input>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="onSubmit">确定</el-button>
          <el-button @click="subCancel">取消</el-button>
        </el-form-item>
      </el-form>
    </el-drawer>
  </div>
</template>

<script>
import {
  boxRewardTypeList,
  addBoxRewardType,
  editBoxRewardType,
  delBoxRewardType
} from '@/api/boxreward'
import { parseTime } from '@/utils/index'

export default {
  name: 'BoxRewardTypeList',
  data() {
    return {
      baseApi: process.env.VUE_APP_BASE_API,
      // 分页数据
      total: 0,
      currentPage: 1,
      pageSize: 10,
      //input
      query: {
        keywords: ''
      },
      tableData: [],
      // 编辑抽屉
      drawer: false,
      direction: 'rtl',
      title: '编辑',
      form: {
        class_name: '',
        sort: ''
      },
      rules: {
        class_name: [
          { required: true, message: '请输入奖品名称', trigger: 'blur' }
        ],
        sort: [{ required: true, message: '请选择奖品分类', trigger: 'blur' }]
      },
      loading: false
    }
  },
  mounted() {
    this.getBoxRewardTypeList()
  },
  methods: {
    // 获取登录日志
    async getBoxRewardTypeList() {
      this.loading = true
      const res = await boxRewardTypeList({
        page: this.currentPage,
        limit: this.pageSize,
        keywords: this.query.keywords
      })
      this.loading = false
      console.log(res)
      this.tableData = res.data
      this.tableData.forEach((item) => {
        if (item.add_time == 0) {
          item.add_time = '-'
        } else {
          item.add_time = parseTime(item.add_time, '{y}-{m}-{d} {h}:{i}:{s}')
        }
        if (item.update_time == 0) {
          item.update_time = '-'
        } else {
          item.update_time = parseTime(
            item.update_time,
            '{y}-{m}-{d} {h}:{i}:{s}'
          )
        }
      })
      this.total = res.count
    },
    queryAsk() {
      this.currentPage = 1
      this.getBoxRewardTypeList()
    },
    //重置
    queryReset() {
      this.resetForm('query')
      this.total = 0
      this.currentPage = 1
      this.pageSize = 10
      this.query.keywords = ''
      this.getBoxRewardTypeList()
    },
    addList() {
      this.drawer = true
      this.title = '添加'
    },
    // 表格操作
    async handleDel(row) {
      try {
        const res = await delBoxRewardType({ id: row.id })
        console.log(res)
        this.getBoxRewardTypeList()
        this.$message({
          message: '删除成功',
          type: 'success'
        })
      } catch (error) {
        this.$message.error('删除失败')
        console.log(error)
      }
    },
    handleEdite(row) {
      this.drawer = true
      this.title = '编辑'
      console.log(row)
      this.form.class_name = row.class_name
      this.form.sort = row.sort
      this.form.id = row.id
    },
    //分页改变
    currentChange(current) {
      this.currentPage = current
      this.getBoxRewardTypeList()
    },
    //抽屉
    handleClose(done) {
      this.resetForm('form')
      const form = {
        class_name: '',
        sort: ''
      }
      this.form = form
      this.drawer = false
      done()
    },
    subCancel() {
      this.resetForm('form')
      const form = {
        class_name: '',
        sort: ''
      }
      this.form = form
      this.drawer = false
    },
    onSubmit() {
      this.$refs['form'].validate(async (valid) => {
        if (valid) {
          if (this.title === '添加') {
            console.log(this.form)
            const res = await addBoxRewardType({ ...this.form })
            console.log(res)
            if (res.st === 1) {
              this.resetForm('form')
              const form = {
                class_name: '',
                sort: ''
              }
              this.form = form
              this.getBoxRewardTypeList()
              this.$message({
                message: '添加成功',
                type: 'success'
              })
              this.drawer = false
            } else {
              console.log(res)
              this.$message.error('添加失败')
            }
          } else if (this.title === '编辑') {
            const res = await editBoxRewardType({ ...this.form })
            console.log(res)
            if (res.st === 1) {
              this.resetForm('form')
              const form = {
                class_name: '',
                sort: ''
              }
              this.form = form
              this.getBoxRewardTypeList()
              this.$message({
                message: '编辑成功',
                type: 'success'
              })
              this.drawer = false
            } else {
              console.log(res)
              this.$message.error('编辑失败')
            }
          }
        }
      })
    },
    // 重置表单
    resetForm(formName) {
      this.$refs[formName].resetFields()
      this.drawer = false
    }
  }
}
</script>

<style lang='scss' scoped>
.query-card {
  display: flex;
}
.box-card {
  margin-bottom: 20px;
}
.search {
  display: flex;
  align-items: center;
}
::v-deep .el-textarea__inner {
  width: 360px !important;
}
::v-deep .el-drawer.rtl {
  width: 650px !important;
  overflow: auto;
}
</style>